package com.mrzhou.auth.controller;

import com.mrzhou.auth.utils.TokenUtil;
import com.mrzhou.auth.utils.VerifyCodeUtil;
import com.mrzhou.core.tool.api.CloudUser;
import com.mrzhou.core.tool.api.R;
import com.mrzhou.core.tool.utils.DataUtil;
import com.mrzhou.core.tool.utils.Func;
import com.mrzhou.web.util.WebUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.entity.ContentType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@Slf4j
public class AuthErrorController {

    @RequestMapping(TokenUtil.ERROR_HANDLER_URL)
    @ApiOperation(value = "处理OAuth2.0过滤器中的异常", hidden = true)
    public R<String> authError(HttpServletRequest request) {
        return R.fail(Func.toStr(request.getAttribute(TokenUtil.ERROR_PARAM)));
    }

    @GetMapping("user/logout")
    @ApiOperation(value = "账号登出")
    public R<String> logout(CloudUser cloudUser) {
        return R.success("登出成功");
    }

    @GetMapping("user/get-image-code")
    @ApiOperation(value = "获取登录的图形验证码")
    public R<String> getImageCode(HttpServletResponse response) {
        try {
            response.setContentType(ContentType.IMAGE_PNG.getMimeType());
            VerifyCodeUtil.imageCode(450, 150, response.getOutputStream(),5);
        } catch (Exception e) {
            log.error("获取验证码失败: {}", e);
            response.setContentType(ContentType.APPLICATION_JSON.getMimeType());
            return R.fail("获取验证码失败");
        }
        response.setContentType(ContentType.APPLICATION_JSON.getMimeType());
        return R.success("获取验证码成功");
    }

}
